package zcj.stu.controller;

import org.springframework.web.bind.annotation.*;
import zcj.stu.Entity.Order;
import zcj.stu.service.CommonService;
import zcj.stu.service.OrderService;
import zcj.stu.utils.Result;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private CommonService commonService;
    @GetMapping("/goods/sale/statistics")
    public Result saleStatistics(){
        try {
            return orderService.saleStatistics();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @GetMapping("/getOrderPage")
    public Result getGoodsPage(int currentPage, int pageSize,String search) {
        try {
            return commonService.getAccountPage(currentPage, pageSize, Order.class,search);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @PostMapping("/status/update")
    public Result updateStatus(@RequestBody Map<String,String>map){
        try {
            System.out.println(map);
            boolean isSuccess =orderService.updateStatus(map);
            if (isSuccess) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @GetMapping("/getOrderByStatus")
    public Result getOrderByStatus(String openid,String status){
        try {
            System.out.println("openid:"+openid+"  "+"status:"+status);
            List<Order>orders=orderService.getOrderByStatus(openid,status);
            return Result.ok(orders);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @PostMapping("/add")
    public Result add(@RequestBody Order order){
        try {
            return orderService.add(order);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
}
